package com.ocom.human.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.request.human.FaceFeatureRequest;
import com.ocom.common.request.human.FeatureRequest;
import com.ocom.common.request.human.GetPerFeatureRequest;
import com.ocom.common.request.human.GetPerFeatureResponse;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.human.service.PersonnelFaceFeatureService;
import com.ocom.human.service.PersonnelFeatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class PersonnelFeatureController {

    @Autowired
    private PersonnelFeatureService personnelFeatureService;

    @Autowired
    private PersonnelFaceFeatureService personnelFaceFeatureService;

    @PostMapping(HumanServiceUrl.URL_PersonnelGetPerFeature)
    public Result personnelGetPerFeature(@RequestBody GetPerFeatureRequest request) {

        return ResultUtil.success(personnelFeatureService.getPerFeature(request.getComId(), request));
    }

    @PostMapping(HumanServiceUrl.URL_PersonnelReset)
    public Result personnelReset(@RequestBody GetPerFeatureRequest request) {

        request.setPage(1);
        request.setSize(10000);
        Map<String, Object> perFeature = personnelFeatureService.getPerFeature(request.getComId(), request);
        Object list = perFeature.get("list");
        List<GetPerFeatureResponse> featureResponses = JSON.parseArray(JSON.toJSONString(list), GetPerFeatureResponse.class);
        for (GetPerFeatureResponse response : featureResponses) {

            FeatureRequest featureRequest = new FeatureRequest();
            featureRequest.setComId(request.getComId());
            featureRequest.setPId(response.getPId());
            featureRequest.setUrl(response.getPUrl());
            featureRequest.setUrl2(response.getPUrl2());
            featureRequest.setVersion(response.getVersion());
            personnelFeatureService.feature(request.getComId(), featureRequest);
        }
        return ResultUtil.success();
    }

    @PostMapping(HumanServiceUrl.URL_PersonnelFeature)
    public Result feature(@RequestBody FeatureRequest request) {
        personnelFeatureService.feature(request.getComId(), request);
        return ResultUtil.success();
    }


    @PostMapping(HumanServiceUrl.URL_PersonnelPhotoFeature)
    public Result featureList(@RequestBody FaceFeatureRequest request) {
        return ResultUtil.success(personnelFaceFeatureService.featureList(request.getComId(), request));
    }





}
